package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import pojo.Books;
import service.BookService;

import javax.management.Query;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/")
public class BookController {
    //controller调service层
    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    //查询全部书籍，并且返回到一个书籍展示页面
    @RequestMapping("/allBook")
    public String list(Model model){
        List<Books> list= bookService.queryAllBook();
        model.addAttribute("list",list);
        for (Books books : list) {
            System.out.println(books);
        }
        return "allBook";
    }

    //跳转到增加书籍页面
    @RequestMapping("/toAddBook")
    public String toAddPaper(){
        return "addBook";
    }
    //添加书籍的请求
    @RequestMapping("/addBook")
    public String addBook(Books books){
        System.out.println("addBook=>"+books);
        bookService.addBook(books);
        return "redirect:/allBook";//(重定向到allbook主页面，实现了请求复用)
    }

    //跳转到修改页面
    @RequestMapping("/toUpdate")
    public String toUpdate(int id,Model model){
        Books books = bookService.queryBookById(id);
        model.addAttribute("ABOK",books);
        return "update";
    }

   // 更新书本信息功能
    @RequestMapping("/Update")
    public String UpdateBook(Books books){
        System.out.println("updateBook=>"+books);
    bookService.updateBook(books);
        return "redirect:/allBook";
    }

    //删除书籍的功能
    @RequestMapping("/Delete")
    public String Delete(int id){
        bookService.deleteBookById(id);
        return "redirect:/allBook";
    }

    //查询书籍功能
    @RequestMapping("/QueryBook")
    public String QueryBook(String queryBookName,Model model){
        Books books = bookService.queryByName(queryBookName);
        System.err.println("queryBook=>"+books);

       List<Books> books1 = new ArrayList<Books>();

        books1.add(books);
        model.addAttribute("list",books1);
        return "allBook";
    }
}